.TH std::chrono::abs(std::chrono::duration) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::chrono::abs(std::chrono::duration) \- std::chrono::abs(std::chrono::duration)

.SH Synopsis
   Defined in header <chrono>
   template< class Rep, class Period >
   constexpr std::chrono::duration<Rep, Period> abs(                      \fI(since C++17)\fP
   std::chrono::duration<Rep, Period> d );

   Returns the absolute value of the duration d. Specifically, if d >= d.zero(), return
   d, otherwise return -d.

   The function does not participate in the overload resolution unless
   std::numeric_limits<Rep>::is_signed is true.

.SH Parameters

   d - duration

.SH Return value

   Absolute value of d.

.SH Possible implementation

   template<class Rep, class Period,
       class = std::enable_if_t<std::numeric_limits<Rep>::is_signed>>
   constexpr std::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d)
   {
       return d >= d.zero() ? +d : -d;
   }

.SH Example


// Run this code

 #include <chrono>
 #include <iostream>

 int main()
 {
     using namespace std::chrono;

     static_assert(abs(-42s) == std::chrono::abs(42s));

     std::cout << "abs(+3min) = " << abs(3min).count() << '\\n'
               << "abs(-3min) = " << abs(-3min).count() << '\\n';
 }

.SH Output:

 abs(+3min) = 3
 abs(-3min) = 3

.SH See also

   operator+ implements unary + and unary -
   operator- \fI(public member function)\fP
   abs(int)
   labs      computes absolute value of an integral value (\\(\\small{|x|}\\)|x|)
   llabs     \fI(function)\fP
   \fI(C++11)\fP
